var Message = require('../models/Message');
var User = require('../models/User');
var Activity = require('../models/Activity');
var Prize = require('../models/Prize');
exports.answer = function(data,callback){
	// console.log(data)
	switch(data.MsgType[0]){
		case 'text':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.Content = data.Content[0];
			_message.MsgId = data.MsgId[0];


			_message.save(function(err,$message){
				var time = Math.round(new Date().getTime()/1000)
				if(data.Content[0]=='2016新年快乐'){
					var msg = {
							"toUserName" : data.FromUserName[0],
							"fromUserName" : data.ToUserName[0]
						}

					var output=""+"<xml>" + 
							 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
							 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
							 "<CreateTime>" + time + "</CreateTime>" + 
							 "<MsgType><![CDATA[news]]></MsgType>" + 
							 "<ArticleCount>1</ArticleCount>" + 
							 "<Articles>"+
							 	"<item>"+
							 	"<Title><![CDATA[兑奖 | TopXGun拓攻2016猴年--快来领取您的奖品！]]></Title>"+
							 	"<Description><![[2016猴年到，在新年的开始，恭喜您获得了TopXGun拓攻2016年的新春大礼，下面拓攻小编就大家详细介绍活动的兑奖规则！]]></Description>"+
							 	"<PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz/GyKNiayNR6Roicl8Bblxftibd9OoOJX4dcsicHZoE7lFJCoVb9VHBIuIIOpWtQdRtjBwMLyz2GgmfWoRmxppvjGiawQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1]]></PicUrl>"+
							 	"<Url><![CDATA[http://mp.weixin.qq.com/s?__biz=MzA4NDk4MzA4NQ==&mid=402263173&idx=1&sn=000981bd94f5e6f4a678ebe88eb27ef4#rd]]></Url>"+
							 	"</item>"+
							 "</Articles>"+
						"</xml>";
					callback(null,output)
				}else{
					var msg = {
							"toUserName" : data.FromUserName[0],
							"fromUserName" : data.ToUserName[0],
							"content":"感谢您的回复..."
						}

					var output=""+"<xml>" + 
							 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
							 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
							 "<CreateTime>" + time + "</CreateTime>" + 
							 "<MsgType><![CDATA[text]]></MsgType>" + 
							 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
						"</xml>";
					callback(null,output)
				}
			})
		};break;
		case 'image':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.PicUrl = data.PicUrl[0];
			_message.MediaId = data.MediaId[0];
			_message.save(function(err,$message){
				var time = Math.round(new Date().getTime()/1000)
				var msg = {
						"toUserName" : data.FromUserName[0],
						"fromUserName" : data.ToUserName[0],
						"content":"一条图片消息（来自 小拓）"
					}

				var output=""+"<xml>" + 
						 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
						 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
						 "<CreateTime>" + time + "</CreateTime>" + 
						 "<MsgType><![CDATA[text]]></MsgType>" + 
						 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
					"</xml>";
				callback(null,output)
			})
		};break;
		case 'voice':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.MediaId = data.MediaId[0];
			_message.Format = data.Format[0];
			_message.Recognition = data.Recognition ?data.Recognition[0]:'';

			_message.save(function(err,$message){
				var time = Math.round(new Date().getTime()/1000)
				var msg = {
						"toUserName" : data.FromUserName[0],
						"fromUserName" : data.ToUserName[0],
						"content":"一条语音消息（来自 小拓）"
					}

				var output=""+"<xml>" + 
						 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
						 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
						 "<CreateTime>" + time + "</CreateTime>" + 
						 "<MsgType><![CDATA[text]]></MsgType>" + 
						 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
					"</xml>";
				callback(null,output)
			})

		};break;
		case 'video':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.MediaId = data.MediaId[0];
			_message.ThumbMediaId = data.ThumbMediaId[0];
			_message.save(function(err,$message){
			var time = Math.round(new Date().getTime()/1000)
			var msg = {
					"toUserName" : data.FromUserName[0],
					"fromUserName" : data.ToUserName[0],
					"content":"一条视频消息（来自 小拓）"
				}

			var output=""+"<xml>" + 
					 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
					 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
					 "<CreateTime>" + time + "</CreateTime>" + 
					 "<MsgType><![CDATA[text]]></MsgType>" + 
					 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
				"</xml>";
			callback(null,output)
		})

		};break;
		case 'shortvideo':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.MediaId = data.MediaId[0];
			_message.ThumbMediaId = data.ThumbMediaId[0];
			_message.save(function(err,$message){
			var time = Math.round(new Date().getTime()/1000)
			var msg = {
					"toUserName" : data.FromUserName[0],
					"fromUserName" : data.ToUserName[0],
					"content":"一条小视频消息（来自 小拓）"
				}

			var output=""+"<xml>" + 
					 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
					 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
					 "<CreateTime>" + time + "</CreateTime>" + 
					 "<MsgType><![CDATA[text]]></MsgType>" + 
					 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
				"</xml>";
			callback(null,output)
		})

		};break;
		case 'location':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.Location_X = data.Location_X[0];
			_message.Location_Y = data.Location_Y[0];
			_message.Scale = data.Scale[0];
			_message.Label = data.Label[0];
			_message.save(function(err,$message){
			var time = Math.round(new Date().getTime()/1000)
			var msg = {
					"toUserName" : data.FromUserName[0],
					"fromUserName" : data.ToUserName[0],
					"content":"一条地理位置消息（来自 小拓）"
				}

			var output=""+"<xml>" + 
					 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
					 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
					 "<CreateTime>" + time + "</CreateTime>" + 
					 "<MsgType><![CDATA[text]]></MsgType>" + 
					 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
				"</xml>";
			callback(null,output)
		})

		};break;
		case 'link':{
			var _message = new Message();
			_message.ToUserName = data.ToUserName[0];
			_message.FromUserName = data.FromUserName[0];
			_message.CreateTime = data.CreateTime[0];
			_message.MsgType = data.MsgType[0];
			_message.MsgId = data.MsgId[0];
			_message.Title = data.Title[0];
			_message.Description = data.Description[0];
			_message.Url = data.Url[0];
			_message.save(function(err,$message){
			var time = Math.round(new Date().getTime()/1000)
			var msg = {
					"toUserName" : data.FromUserName[0],
					"fromUserName" : data.ToUserName[0],
					"content":"收到一条跳转链接消息（来自 小拓）"
				}

			var output=""+"<xml>" + 
					 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
					 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
					 "<CreateTime>" + time + "</CreateTime>" + 
					 "<MsgType><![CDATA[text]]></MsgType>" + 
					 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
				"</xml>";
			callback(null,output)
		})

		};break;
		case 'event':{
			switch(data.Event[0]){
				case 'subscribe':{
					
				};break;
				case 'unsubscribe':{


				};break;
				case 'SCAN':{

				};break;
				case 'LOCATION':{

				};break;
				case 'CLICK':{
					switch(data.EventKey[0]){
						case 'TG_TEST':{
							var time = Math.round(new Date().getTime()/1000)
							var msg = {
									"toUserName" : data.FromUserName[0],
									"fromUserName" : data.ToUserName[0],
									"content":"Hi,我是拓攻的小拓,最6的机器人!"
								}

							var output=""+"<xml>" + 
									 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
									 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
									 "<CreateTime>" + time + "</CreateTime>" + 
									 "<MsgType><![CDATA[text]]></MsgType>" + 
									 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
								"</xml>";
							callback(null,output)
						};break;
						case 'TG_CONTACT':{
							var time = Math.round(new Date().getTime()/1000)
							var msg = {
									"toUserName" : data.FromUserName[0],
									"fromUserName" : data.ToUserName[0],
									"content":"官方电话: 400-7883426        (1)售后服务热线: 025-83798727 / 400-7883426    (2)售后服务在线QQ: 1035064249 / 3083949336 / 3312221701                       (3)售后服务邮箱: support@topxgun.com          (4)售后问题交流群: 413560286"
								}

							var output=""+"<xml>" + 
									 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
									 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
									 "<CreateTime>" + time + "</CreateTime>" + 
									 "<MsgType><![CDATA[text]]></MsgType>" + 
									 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
								"</xml>";
							callback(null,output)
						};break;
						case 'TG_MOVEUP':{
							var time = Math.round(new Date().getTime()/1000)
							var msg = {
									"toUserName" : data.FromUserName[0],
									"fromUserName" : data.ToUserName[0],
									"content":"您好！请回复贵公司的名称、地址、联系人、联系方式、问题描述以及希望大篷车抵达的时间"
								}

							var output=""+"<xml>" + 
									 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
									 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
									 "<CreateTime>" + time + "</CreateTime>" + 
									 "<MsgType><![CDATA[text]]></MsgType>" + 
									 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
								"</xml>";
							callback(null,output)
						};break;
						case 'CZ_PRIZE':{
							var time = Math.round(new Date().getTime()/1000)
							var msg = {
									"toUserName" : data.FromUserName[0],
									"fromUserName" : data.ToUserName[0],
									"content":"系统正在处理您的请求"
							}

							User.findOne({openid:data.FromUserName[0]},function(err,user){
								if(err){
									msg.content = "系统繁忙，请稍后再试"
									var output=""+"<xml>" + 
											 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
											 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
											 "<CreateTime>" + time + "</CreateTime>" + 
											 "<MsgType><![CDATA[text]]></MsgType>" + 
											 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
										"</xml>";
									callback(null,output)
								}else{
									if(user){
										Prize.findOne({user_id:user._id},function(err,prize){
											if(err){
												msg.content = "系统繁忙，请稍后再试"
												var output=""+"<xml>" + 
														 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
														 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
														 "<CreateTime>" + time + "</CreateTime>" + 
														 "<MsgType><![CDATA[text]]></MsgType>" + 
														 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
													"</xml>";
												callback(null,output)
											}else{
												if(prize){
													msg.content = "恭喜您!获得了TopXGun拓攻2016年双11活动的"+prize.code+"["+prize.desp+"]!中奖电子码："+prize._id+"!"
													var output=""+"<xml>" + 
															 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
															 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
															 "<CreateTime>" + time + "</CreateTime>" + 
															 "<MsgType><![CDATA[text]]></MsgType>" + 
															 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
														"</xml>";
													callback(null,output)
												}else{
													msg.content = "抱歉，您没有中奖"
													var output=""+"<xml>" + 
															 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
															 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
															 "<CreateTime>" + time + "</CreateTime>" + 
															 "<MsgType><![CDATA[text]]></MsgType>" + 
															 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
														"</xml>";
													callback(null,output)
												}
												
											}
											
										})
									}else{
										msg.content = "抱歉，服务器没有查到任何关于您的信息"
										var output=""+"<xml>" + 
												 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
												 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
												 "<CreateTime>" + time + "</CreateTime>" + 
												 "<MsgType><![CDATA[text]]></MsgType>" + 
												 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
											"</xml>";
										callback(null,output)
										
									}
									
								}
							
							})


						};break;
						default:{
							var time = Math.round(new Date().getTime()/1000)
							var msg = {
									"toUserName" : data.FromUserName[0],
									"fromUserName" : data.ToUserName[0],
									"content":"一条事件消息（来自 小拓）"
								}

							var output=""+"<xml>" + 
									 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
									 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
									 "<CreateTime>" + time + "</CreateTime>" + 
									 "<MsgType><![CDATA[text]]></MsgType>" + 
									 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
								"</xml>";
							callback(null,output)
						};break;
					}
				};break;
				case 'VIEW':{

				};break;
				default:{
					var time = Math.round(new Date().getTime()/1000)
					var msg = {
							"toUserName" : data.FromUserName[0],
							"fromUserName" : data.ToUserName[0],
							"content":"拓攻的客服将会尽快为您解答（来自 小拓）"
						}

					var output=""+"<xml>" + 
							 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
							 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
							 "<CreateTime>" + time + "</CreateTime>" + 
							 "<MsgType><![CDATA[text]]></MsgType>" + 
							 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
						"</xml>";
					callback(null,output)
				};break;

			}

			
		};break;
		default:{
			var time = Math.round(new Date().getTime()/1000)
			var msg = {
					"toUserName" : data.FromUserName[0],
					"fromUserName" : data.ToUserName[0],
					"content":"拓攻的客服将会尽快为您解答（来自 小拓）"
				}

			var output=""+"<xml>" + 
					 "<ToUserName><![CDATA[" + msg.toUserName + "]]></ToUserName>" + 
					 "<FromUserName><![CDATA[" + msg.fromUserName + "]]></FromUserName>" + 
					 "<CreateTime>" + time + "</CreateTime>" + 
					 "<MsgType><![CDATA[text]]></MsgType>" + 
					 "<Content><![CDATA[" + msg.content + "]]></Content>" + 
				"</xml>";
			callback(null,output)
		}break;
	}

}